package com.lxujia.ruankao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lxujia.ruankao.R;
import com.lxujia.ruankao.activity.QuestionItemActivity;
import com.lxujia.ruankao.adapter.CategoryListViewAdapter;
import com.lxujia.ruankao.model.CategoryItem;
import com.lxujia.ruankao.model.CategoryType;
import com.lxujia.ruankao.model.QuestionItem;
import com.lxujia.ruankao.util.LogUtil;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class ErrorQuestionFragment extends Fragment {
    private final static String TAG = "OldExamsFragment";
    private String mCategoryType = "pm";
    private List<CategoryItem> mCategoryItemList = new ArrayList<>();

    private CategoryListViewAdapter mAdapter;
    private View mFragmentView;

    public ErrorQuestionFragment() {}

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mFragmentView = inflater.inflate(R.layout.fragment_category_list, container,false);

        initData();

        return mFragmentView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        //可见时重新加载
        if (isVisibleToUser && isVisible()) {
            initData();
        }
    }

    private void initData() {
        // 由于 categoryType 唯一，查一行就可以了。并查出关联的 CategoryItem
        CategoryType categoryType = LitePal.where("categoryType == ?", mCategoryType).
                findFirst(CategoryType.class);

        String linkId = CategoryType.class.getSimpleName().toLowerCase();
        List<CategoryItem> categoryItems = LitePal.where(linkId + "_id == ?",
                String.valueOf(categoryType.getId())).order("period desc").find(CategoryItem.class);

        mCategoryItemList.clear();
        for (CategoryItem categoryItem : categoryItems) {
            String conditions = CategoryItem.class.getSimpleName().toLowerCase()
                    + "_id == ?  and isError == 1";
            int count = LitePal.where(conditions,
                    String.valueOf(categoryItem.getId())).order("no asc").count(QuestionItem.class);
            LogUtil.d(TAG, categoryItem.getPeriodToShow() + " 错题数量 ： " + count);
            if (count > 0) {
                mCategoryItemList.add(categoryItem);
            }
        }
        if (mAdapter != null) {
            mAdapter.setData(mCategoryItemList);
        }
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        TextView nameTx = (TextView) getView().findViewById(R.id.category_name_tv);
        nameTx.setText(R.string.error_questions_category);

        ListView listView = (ListView) getView().findViewById(R.id.category_list);
        mAdapter = new CategoryListViewAdapter(getActivity(), false);
        mAdapter.setData(mCategoryItemList);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                final Integer categoryId = mCategoryItemList.get(i).getId();

                Intent intent = new Intent(getActivity(), QuestionItemActivity.class);
                intent.putExtra("categoryId", categoryId);
                intent.putExtra("isError", true);
                startActivity(intent);
            }
        });
        listView.setAdapter(mAdapter);
    }
}
